package com.java.mongodbtest.controller;

import com.java.mongodbtest.entity.Comment;
import com.java.mongodbtest.service.CommentService;
import com.java.mongodbtest.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author huxb
 * @date 2020/9/18 0018 下午 2:01
 * @Desciption
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("/all")
    public Result findAll() {
        List<Comment> commentList = commentService.findAll();
        return Result.success(commentList);
    }

    @RequestMapping("/detail")
    public Result findDetailById(@RequestParam String id) {
        Comment comment = commentService.findDetailById(id);
        return Result.success(comment);
    }

    /**
     * 保存
     * @param comment
     * @return
     */
    @RequestMapping("/save")
    public Result save(@RequestBody Comment comment) {
        commentService.save(comment);
        return  Result.success(null);
    }

    /**
     * 编辑
     * @param comment
     * @return
     */
    @RequestMapping("/edit")
    public Result update(@RequestBody Comment comment) {
        commentService.update(comment);
        return Result.success(null);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(@RequestParam String id) {
        commentService.delete(id);
        return Result.success(null);
    }

    /**
     *
     * @param articleId
     * @return
     */
    @RequestMapping("/listByArtId")
    public Result findListByArticleId(@RequestParam String articleId) {
        List<Comment> commentList = commentService.findByArticleId(articleId);
        return  Result.success(commentList);
    }

    /**
     * 分页查询
     * @param pageSize
     * @param pageNumber
     * @param conent
     * @param userId
     * @param startThumbup
     * @param endThumbup
     * @return
     */
    @RequestMapping("/page")
    public Result findPageBy(@RequestParam(required =  false) Integer pageSize, Integer pageNumber, String conent, String userId, Integer startThumbup, Integer endThumbup ) {
        Page<Comment> commentPage = commentService.findPageBy(pageSize, pageNumber, conent, userId, startThumbup, endThumbup);
        return  Result.success(commentPage);
    }

    /**
     * 点赞
     * @param id
     * @return
     */
    @RequestMapping("/thumbup")
    public Result thumbup(String id) {
        commentService.thumbup(id);
        return Result.success(null);
    }
}
